package com.wgts.wgts.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.umeng.analytics.MobclickAgent;
import com.wgts.wgts.ActivityStack;
import com.wgts.wgts.R;
import com.wgts.wgts.control.dialog.WonderLoadingDialog;
import com.wgts.wgts.control.statusBar.StatusBar;
import com.wgts.wgts.tools.InstallUtil;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;


public abstract class BaseActivity extends SwipeBackActivity{

    private SwipeBackLayout mSwipeBackLayout;

    private LinearLayout mlinearLayoutLoading;

    private ImageView loadingView;

    ImageView imageView;

    protected WonderLoadingDialog dialog;

    /**
     * this function need init data;
     */
    public abstract void init();

    @Override
    protected void onCreate(Bundle saveInstanceBundle){
        super.onCreate(saveInstanceBundle);
        StatusBar.transparencyBar(this); // 沉浸式状态栏
        StatusBar.setStatusBarLightMode(this);//状态栏字体图标颜色
        mSwipeBackLayout = getSwipeBackLayout();
        //设置滑动方向，可设置EDGE_LEFT, EDGE_RIGHT, EDGE_ALL, EDGE_BOTTOM
        mSwipeBackLayout.setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
        ActivityStack.getInstance().addActivity(this);
        dimiss();
        init();
    }

    public void showDialog() {
        if (dialog == null) {
            dialog = new WonderLoadingDialog(this);
        // 显示的时候需要show()出来  
            dialog.show();
        } else {
            dialog.show();
        }
    }

    public void dialogDismiss(){
        if (dialog != null && dialog.isShowing()){
            dialog.dismiss();
        }
    }

    @Override
    public void onDestroy(){
        super.onDestroy();
        ActivityStack.getInstance().removeActivity(this);
    }

    @Override
    public void finish(){
        super.finish();
        if(dialog!=null){
            if(dialog.isShowing()){
                dialog.dismiss();
                dialog=null;
            }
        }
    }
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    public void onRefresh(){
        FrameLayout contentView =  (FrameLayout)getWindow().getDecorView().findViewById(android.R.id.content);
        imageView = new ImageView(this);
        imageView.setImageResource(R.mipmap.ip_icon);
        contentView.addView(imageView);
    }

    public void onClose(){
        imageView.setVisibility(View.GONE);
    }




    //键盘不被遮挡
    public void dimiss(){
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    //弹出框与软键盘遮盖问题
    public void textDismiss(View Sview) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(Sview.getWindowToken(), 0);
    }




    /*----------------------------------------点击外部区域软键盘消失------------------------------------------*/

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
       if (ev.getAction() == MotionEvent.ACTION_DOWN) {
          View v = getCurrentFocus();
          if (isShouldHideKeyboard(v, ev)) {
              hideKeyboard(v.getWindowToken());
          }
      }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    private boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     * @param token
     */
    private void hideKeyboard(IBinder token) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    /*----------------------------------------点击外部区域软键盘消失------------------------------------------*/


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == InstallUtil.UNKNOWN_CODE) {
            InstallUtil.install();//再次执行安装流程，包含权限判等
        }
    }

}
